<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>${SYSTEM_NAME}</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/style/admin.css" media="all">
  <link rel="stylesheet" href="${pageContext.request.contextPath}/static/js/zTree_v3/css/metroStyle/metroStyle.css?v=${FILE_VERSION}" type="text/css">
</head>
<body>
 <input type="text" id="userId" style="display: none;"  value="${userId }"/>
 <div class="layui-form" style="padding-top:10px;padding-left: 20px">
 	<div class="layui-form-item" id="roleList">
	      
	</div>
</div>
 <div class="layui-input-block" style="padding-top: 10px">
      <button class="layui-btn layui-btn-primary layui-border-blue" type="button" onclick="userRole()">设置</button>
      <button type="button" class="layui-btn layui-btn-primary layui-border-red" onclick="closed()">关闭</button>
</div>

<script src="${pageContext.request.contextPath}/static/js/common-2.0.0.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/layuiadmin/layui/layui.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/js/jquery-3.3.1.min.js?v=${FILE_VERSION}"></script>
		
<script>
var layer;
layui.config({
    base: getContextPath() + '/static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['layer','form'], function(){
	layer = layui.layer;
	var form = layui.form;
	
	$ajax({
		url: "/sys/user/add/userRole",
		data: {
			"userId" : $("#userId").val()
        },
		async: false,
		isShowLayer : false,
        succCallback: function (json) {
		    console.log(json);
        	if(json.code == 0){
	    		var roleList = json.data.roleList;
                var userRoleList = json.data.userRoleList;
	    		var inputStr = "";
	    		for (var i = 0; i < roleList.length; i++) {
	    			inputStr += '<input type="checkbox" name="roleId" title="' 
	    				+ roleList[i].roleName + 
	    				'" value="' + roleList[i].id + '"';
	    			for(var j = 0; j < userRoleList.length; j++){
	    				 if(userRoleList[j].id == roleList[i].id){
	    					 inputStr += ' checked ';
	    					 break;
	    				 }
	    			}
	    			inputStr += '>';
				}
	    		$("#roleList").html(inputStr);
	    		form.render();
	    	}else{
        	    layer.msg(json.msg);
            }
        }
	});
});

function userRole(){
	var ids = "";
	$('input[name="roleId"]:checked').each(function(){
        ids += $(this).val() + ",";
    });
	ids = ids.substr(0, ids.length - 1);
	$ajax({
		url: "/sys/user/add/setUserRole",
		data: {
        	"userId" : $("#userId").val(),
        	"roleIds" : ids
        },
        succCallback: function (json) {
        	if(json.code == 0){
        		var index = parent.layer.getFrameIndex(window.name); //获取窗口索引  
        		parent.layer.close(index);//关闭弹出的子页面窗口
        		parent.layer.msg(json.msg);
        		parent.reload();
        	}
        }
	});
}

function closed(){
	var index = parent.layer.getFrameIndex(window.name); //获取窗口索引  
	parent.layer.close(index);//关闭弹出的子页面窗口
}

</script>
</body>
</html>